package leecode

import (
	"strconv"
)

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func sumNumbers(root *TreeNode) int {
	var numStr []string
	var backtracking func(node *TreeNode, path string)

	backtracking = func(node *TreeNode, path string) {
		if node == nil {
			return
		}

		path += strconv.Itoa(node.Val)
		if node.Left == nil && node.Right == nil {
			numStr = append(numStr, path)
			return
		}
		backtracking(node.Left, path)
		backtracking(node.Right, path)
	}

	backtracking(root, "")

	var result int
	for _, num := range numStr {
		temp, _ := strconv.Atoi(num)
		result += temp
	}

	return result
}
